package com.zjeav.lingjiao.ui.register;

import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.request.ChanageMobileRequest;
import com.zjeav.lingjiao.base.request.RegisterRequest;
import com.zjeav.lingjiao.base.request.ValidateCode;
import com.zjeav.lingjiao.base.response.ValidateCodeResponse;
import com.zjeav.lingjiao.base.service.ChanageMobileService;
import com.zjeav.lingjiao.base.service.RegisterService;
import com.zjeav.lingjiao.base.service.RequestCodeService;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

public class CodeModel {
    public void getCode(ValidateCode phone, final OnCodeListener listener) {
        Retrofit retrofit = RetrofitInstance.getJsonInstance();
        RequestCodeService service = retrofit.create(RequestCodeService.class);
        service.getcode(phone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ValidateCodeResponse>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ValidateCodeResponse validateCodeResponseResult) {
                        listener.OnSuccess(validateCodeResponseResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.OnFailure(e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void register(RegisterRequest request, final OnCodeListener listener) {
        Retrofit retrofit = RetrofitInstance.getJsonInstance();
        RegisterService service = retrofit.create(RegisterService.class);
        service.register(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result stringResult) {
                        listener.RegisterSuccess(stringResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.RegisterFail(e );
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void chanagemobile(ChanageMobileRequest request, final OnCodeListener listener) {
        Retrofit retrofit = RetrofitInstance.getJsonInstance();
        ChanageMobileService service = retrofit.create(ChanageMobileService.class);
        service.chanagemobile(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Result stringResult) {
                        listener.RegisterSuccess(stringResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.RegisterFail(e );
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public interface OnCodeListener {
        void OnSuccess(ValidateCodeResponse result);

        void OnFailure(Throwable throwable);

        void RegisterSuccess(Result result);

        void RegisterFail(Throwable throwable);

    }
}
